package com.tarena.lbs.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tarena.lbs.base.response.JsonResult;
import com.tarena.lbs.dto.UserQuery;
import com.tarena.lbs.dto.UserRegParam;
import com.tarena.lbs.dto.UserSelectQuery;
import com.tarena.lbs.vo.UserVO;
import com.tarena.lbs.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("v1/user")
@Slf4j
@Api(tags = "用户管理模块")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ApiOperation(value = "用户登录功能", notes = "根据用户提供的用户名和密码进行登录验证")
    @ApiOperationSupport(order = 10)
    public JsonResult login(
            @ApiParam(value = "用户登录所需的查询参数，包含用户名和密码", required = true)
            @RequestBody UserQuery userQuery) {
        log.debug("用户登录请求参数:{}", userQuery);
        UserVO userVO = userService.login(userQuery);
        return JsonResult.ok(userVO);
    }

    @PostMapping("reg")
    @ApiOperation(value = "用户注册功能", notes = "新用户使用注册参数进行账号注册")
    @ApiOperationSupport(order = 20)
    public JsonResult reg(
            @ApiParam(value = "用户注册所需的参数，包含用户基本信息", required = true)
            @RequestBody UserRegParam userRegParam) {
        log.debug("用户注册请求:{}", userRegParam);
        userService.reg(userRegParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    @ApiOperation(value = "用户查询功能", notes = "根据传入的查询条件查询用户列表")
    @ApiOperationSupport(order = 30)
    public JsonResult select(
            @ApiParam(value = "用户查询参数，可用于筛选用户，如用户名、手机号等", required = false)
            UserSelectQuery userSelectQuery) {
        log.debug("用户查询参数:{}", userSelectQuery);
        List<UserVO> list = userService.select(userSelectQuery);
        return JsonResult.ok(list);
    }
}